<?php

namespace App\Console\Commands;

use App\Models\Report;
use Carbon\Carbon;
use Illuminate\Console\Command;
use App\Models\ParkInfo;
use App\Models\ParkRecord;

/**
 * 过夜车辆查询
 * 凌晨3点执行
 * Class OvernightCar
 * @package App\Console\Commands
 */
class OvernightCar extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'overnight:day-report';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '过夜车报表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $year  = intval(date('Y'));
        $month = intval(date('n'));
        $day   = intval(date('j')) - 1;

        $this->info(date('Y-m-d', mktime(0, 0, 0, $month, $day, $year)));
        $this->_mkdata($day, $month, $year);

        // // dateMap
        // $dateMap = [
        //     2018 => [
        //         7  => 31, 
        //         8  => 31, 
        //         9  => 30, 
        //         10 => 31, 
        //         11 => 30, 
        //         12 => 9,
        //     ],
        // ];

        // // traverse
        // foreach ($dateMap as $y => $mm) {
        //     foreach ($mm as $m => $d) {
        //         for ($i = 1; $i <= $d; $i++) { 
        //             $this->info(sprintf(date('Y-m-d', mktime(0, 0, 0, $m, $i, $y)) ));
        //             $this->_mkdata($i, $m, $y);
        //         }
        //     }
        // }
    }



    /**
     * make data.
     *
     * @return mixed
     */
    private function _mkdata($day = 0, $month = 0, $year = 0)
    {
        //查询当月数据
        $monthFull = date('Y-m', mktime(0, 0, 0, $month, $day, $year));
        // $monthFull = sprintf('%4d-%02d', $year, $month);
        $dayFull   = date('Y-m-d', mktime(0, 0, 0, $month, $day, $year));
        // $dayFull   = sprintf('%4d-%02d-%02d', $year, $month, $day);

        // 一周前0:00 - 3:00 过夜车
        $timeStart = mktime(0, 0, 0, $month, $day, $year);
        $timeEnd   = mktime(3, 0, 0, $month, $day+1, $year);
        $timeEnd   = time();


        $this->info(sprintf('TimeStart: %s', date('Y-m-d H:i:s', $timeStart)));
        $this->info(sprintf('TimeEnd:   %s', date('Y-m-d H:i:s', $timeEnd)));

        $report = Report::where('report_date', $monthFull)
                        ->where('type', 'overnight_count')
                        ->first();
        if (!$report) {
            $report = new Report;
            $report->report_date = $monthFull;
            $report->type = 'overnight_count';
            $report->value = [];
        }

        $parks = ParkInfo::whereIn('park_code', config('custom.report_parks'))
                            ->select(['park_code', 'parking_name'])
                            ->orderBy('sort_by', 'desc')
                            ->get();

        // new data
        $data = [
            'total' => 0,
            'date'  => $dayFull,
            'list'  => [],
        ];
        foreach ($parks as $park) {
            // 去除过夜车
            if (in_array($park->park_code, config('custom.irs_parks'))) {
                continue;
            }

            $parkInfo = ParkInfo::where("park_code", $park->park_code)
                                    ->select("space_total", 'space_empty',"park_code")
                                    ->first();

            // item
            $item = [
                'park_code'    => $park->park_code,
                'parking_name' => $park->parking_name,
                'inner_car'    => $parkInfo->space_total - $parkInfo->space_empty,
            ];

            // 总计
            $data['total'] += $item['inner_car'];
            // return item
            $data['list'][] = $item;
        } 

        $park_inner_arr = array_column($data["list"],"inner_car","park_code");
        $p23_data = 
            ($park_inner_arr['7100000288']-$park_inner_arr['7100000290']-$park_inner_arr['7100000291'])>0?
        ($park_inner_arr['7100000288']-$park_inner_arr['7100000290']-$park_inner_arr['7100000291']):0;

        $data['total'] =  $data['total']-$park_inner_arr['7100000290']-$park_inner_arr['7100000291'];
        foreach($data['list'] as $key=>&$val){
            if($val['park_code'] == 7100000288){
                $val['inner_car'] = $p23_data;
            }
        }


        // 当天数据
        $value = (array)$report->value;
        $value[$dayFull] = $data;
        $report->value = $value;

        $report->save();
        $this->info('场内车计算成功');
    }
    

    // /**
    //  * make data.
    //  *
    //  * @return mixed
    //  */
    // private function _mkdata($day = 0, $month = 0, $year = 0)
    // {
    //     //查询当月数据
    //     $monthFull = sprintf('%4d-%02d', $year, $month);
    //     $dayFull   = sprintf('%4d-%02d-%02d', $year, $month, $day);

    //     // 2:00 - 3:00 过夜车
    //     $timeStart = mktime(3, 0, 0, $month, $day, $year);
    //     $timeEnd   = mktime(3, 0, 0, $month, $day+1, $year);

    //     $report = Report::where('report_date', $monthFull)
    //                     ->where('type', 'overnight_count')
    //                     ->first();
    //     if (!$report) {
    //         $report = new Report;
    //         $report->report_date = $monthFull;
    //         $report->type = 'overnight_count';
    //         $report->value = [];
    //     }

    //     $parks = ParkInfo::whereIn('park_code', config('custom.report_parks'))
    //                         ->select(['park_code', 'parking_name'])
    //                         ->orderBy('sort_by', 'desc')
    //                         ->get();

    //     // new data
    //     $data = [
    //         'total' => 0,
    //         'date'  => $dayFull,
    //         'list'  => [],
    //     ];
    //     foreach ($parks as $park) {
    //         // park record
    //         $parkRecord = new ParkRecord;
    //         $parkRecord->setTable(sprintf('t_park_record_%s', $park->park_code));

    //         // item
    //         $item = [
    //             'park_code'    => $park->park_code,
    //             'parking_name' => $park->parking_name,
    //             // 过夜车：出车时间 >3:00
    //             'inner_car'    => $parkRecord
    //                                 ->whereBetween('in_time', [$timeStart, $timeEnd-1])
    //                                 ->whereRaw('(`out_time` = 0 or `out_time` >= ?)', $timeEnd)
    //                                 ->count(),
    //         ];
    //         // 总计
    //         $data['total'] += $item['inner_car'];
    //         // return item
    //         $data['list'][] = $item;
    //     }

    //     // 当天数据
    //     $value = (array)$report->value;
    //     $value[$dayFull] = $data;
    //     $report->value = $value;

    //     $report->save();
    //     $this->info('场内车计算成功');
    // }
}
